/*
 * SteepTimer - A timer designed for tea fanatics
 * Copyright (C) 2011  Wesley Crossman
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include "TeaSound.h"
#include "CsvParser.h"
#include <Phonon/MediaObject>
#include <Phonon/MediaSource>
#include <Phonon/AudioOutput>
#include <QSettings>
#include <QApplication>
#include <QFile>
#include <QTextStream>

TeaSound *TeaSound::m_instance = NULL;

TeaSound::TeaSound() :
            QObject(NULL),
            m_mobject(NULL),
            m_aoutput(NULL) {
    if (!QDir(".").exists("sounds")) {
        m_soundDir = QDir(QDir(qApp->applicationDirPath()).filePath("sounds"));
    } else {
        m_soundDir = QDir("sounds");
    }

    QFile csv(m_soundDir.filePath("clips.csv"));
    if (csv.open(QIODevice::ReadOnly)) {
        QTextStream stream(&csv);
        QString str = stream.readAll();
        CsvParser csvParser;
        csvParser.parse(str);
        for (int a=0;a<csvParser.rowCount();++a) {
            if (csvParser.columnCount(a) == 5) {
                m_soundList.append(SoundSettings(csvParser.row(a)));
            }
        }
    }
}

void TeaSound::play(const QString &filename) {
    QSettings settings;
    QString sound = filename;
    if (filename.isNull()) {
        sound = settings.value("settings/sound").toString();
    }
    if (!filename.isNull() || settings.value("settings/soundEnabled").toBool()) {
        m_mobject->setCurrentSource(*load(sound));
        m_mobject->stop();
        m_mobject->play();
    }
}

void TeaSound::enqueue(const QString &filename) {
    QSettings settings;
    QString sound = filename;
    if (filename.isNull()) {
        sound = settings.value("settings/sound").toString();
    }
    if (!filename.isNull() || settings.value("settings/soundEnabled").toBool()) {
        m_mobject->enqueue(*load(sound));
    }
}

Phonon::MediaSource *TeaSound::load(const QString &filename) {
    Q_ASSERT(!filename.isNull());
    if (!m_mobject) {
        m_mobject = new Phonon::MediaObject();
        connect( m_mobject, SIGNAL(aboutToFinish()), SIGNAL(aboutToFinish()) );
        m_aoutput = new Phonon::AudioOutput(Phonon::NotificationCategory);
        Phonon::createPath(m_mobject, m_aoutput);
    }
    return new Phonon::MediaSource(m_soundDir.filePath(filename));
}

TeaSound *TeaSound::instance() {
    if (!m_instance) m_instance = new TeaSound();
    return m_instance;
}
